package Message;

import java.util.List;

/**
 * Created by sirius_swu on 2017/3/7.
 */
public class ProductManager {
    private IProductService dao = null;

    public ProductManager() {
        dao = new ProductService();
        System.out.println("Manager" + dao.getAll().size());
    }

    public List<Product> getProductByPager(int pageIndex, int pageSize) {
        System.out.println("ProductManager"+pageIndex);
        return this.dao.getByPager(pageIndex, pageSize);
    }

    public List<Product> getProductByName(String name) {
        return this.dao.getBtName(name);
    }

    public Product getProductById(int id) {
        return this.dao.getById(id);
    }

    public boolean AddProduct(Product p) throws Exception {
        try {
            this.dao.Insert(p);
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean AddProduct(int id, String name, int categoryId, int photo, String unitPrice, String note) throws Exception {
        Product p = new Product(id, name, categoryId, photo, unitPrice, note);
        return this.AddProduct(p);
    }

    public boolean ModifyProduct(Product p) {
        try {
            this.dao.Modify(p);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean ModifyProduct(int id, String name, int categoryId, int photo, String unitPrice, String note) throws Exception {
//        Product p = new Product(id, name, categoryId, photo, unitPrice, note);
        Product p = this.getProductById(id);
        p.setName(name);
        p.setCategoryId(categoryId);
        p.setNote(note);
        p.setPhoto(photo);
        p.setUnitPrice(unitPrice);
        return this.ModifyProduct(p);
    }

    public boolean DelProduct(int id) throws Exception {
        try {
            this.dao.Del(id);
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean DelProduct(Product p) throws Exception {
        return this.DelProduct(p.getId());
    }
    /*
    * 批量删除
    * */
    public boolean DelProduct(List<Integer> ids) {
        try {
            for (Integer id:ids) {
                this.DelProduct(id);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
